package Code.Objects.Structures.Pod;

import java.util.Random;

import org.jrabbit.base.graphics.transforms.Vector2d;
import org.jrabbit.base.managers.ResourceManager;
import org.jrabbit.standard.game.objects.particles.ParticleSprite;
import org.jrabbit.standard.game.objects.particles.controllers.ParticleController;
import org.newdawn.slick.Color;

import Code.Basic.Entity.GenericTerrain;

public class Pod extends GenericTerrain
{
	private static float[][] points2 = new float[][] {{-189, -54}, {-116, -124},
													{16, -151}, {114, -109},
													{180, -19}, {169, 87},
													{85, 207}, {-40, 238},
													{-176, 184}, { -226, 62}};
	
	private ParticleSprite smoke1;
	private ParticleSprite smoke2;
	
	public Pod()
	{
		this(0, 0);
	}
	
	public Pod(double x, double y)
	{
		super("Resources/Images/Objects/Pod/Pod Top.png", points2, x, y, 0);
		smoke1 = new ParticleSprite.Images("Resources/Images/Effects/Smoke/Smoke.png");
		smoke1.location().set(location);
		smoke1.location().add(37, -174);
		smoke2 = new ParticleSprite.Images("Resources/Images/Effects/Smoke/Smoke.png");
		smoke2.location().set(location);
		smoke2.location().add(194, 128);
	}
	
	public boolean isOnscreen()
	{
		return true;
	}
	
	public void render()
	{
		super.render();
		smoke1.render();
		smoke2.render();
	}
	
	public void update(int delta)
	{
		super.update(delta);
		smoke1.addParticle(new SmokeParticle(smoke1));
		smoke2.addParticle(new SmokeParticle(smoke2));
		smoke1.update(delta);
		smoke2.update(delta);
	}
	
	private static class SmokeParticle extends ParticleController
	{
		private static final Color[] colors = new Color[] {Color.black, new Color(0.05f, 0, 0), new Color(0.05f, 0.025f, 0)};
		
		private Vector2d velocity;
		private double wind;
		private double rotationSpeed;
		private float fadeRate;

		public SmokeParticle(ParticleSprite pS)
		{
			super(pS);
			Random r = ResourceManager.random();
			color.set(colors[r.nextInt(colors.length)]);
			location.add((r.nextDouble() - 0.5) * 70, (r.nextDouble() - 0.5) * 70);
			velocity = new Vector2d(0.01 + (r.nextDouble() - 0.5) * 0.01, -r.nextDouble() * 0.02);
			wind = 0.007 * (r.nextDouble() + 1); 
			fadeRate = -0.00003f * (r.nextFloat() + 2);
			scalar.set(2.5f + r.nextFloat());
			rotationSpeed = (r.nextDouble() - 0.5) * 0.02;
			rotation.set(r.nextDouble() * 360);
		}
		
		public void update(int time)
		{
			rotation.rotate(rotationSpeed * time);
			velocity.addX(wind * time * 0.0001);
			location.add(velocity, time);
			
			color.shiftAlpha(fadeRate * time);
			if(color.alpha() <= 0)
			{
				kill();
			}
		}
	}
}
